-------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  /bbkinghome/assarsah/pkeval/ReplicationPackage/Public/Logs/20250929_PMO_merge_long_b1-22.l
> og
  log type:  text
 opened on:  29 Sep 2025, 10:06:58

. 
. /*----------------------------------------------------*/
.             /* Section: Append batches */
. /*----------------------------------------------------*/
. 
. * load first batch
.         u "$KP_deid_admin/Raw/JPAL_DATA_1/cleanedbatch_1.dta", clear

.         gisid anon_id4

. 
. * merge in randomization weight
.         merge 1:1 anon_id4 using "$KP_deid_admin/Raw/JPAL_DATA_2/batch_1.dta", assert(3) nogen

    Result                      Number of obs
    -----------------------------------------
    Not matched                             0
    Matched                         1,871,117  
    -----------------------------------------

.         assert !missing(bobot)

.         rename bobot ticket_score

.         rename test_score final_test_score

. 
. * merge in updated domicili
.         merge 1:1 anon_id4 using "$KP_deid_admin/Raw/JPAL_DATA_10/domisili_1.dta", keep(1 3) keepusin
> g(anon_prov_id java urban) nogen

    Result                      Number of obs
    -----------------------------------------
    Not matched                             0
    Matched                         1,871,117  
    -----------------------------------------

. 
. * merge in additional variables
.         rename has_passed_current_batch has_passed_current_batch_old

.         merge 1:1 anon_id4 using "$KP_deid_admin/Raw/JPAL_DATA_8/additional variable_1.dta"

    Result                      Number of obs
    -----------------------------------------
    Not matched                        18,770
        from master                     1,661  (_merge==1)
        from using                     17,109  (_merge==2)

    Matched                         1,869,456  (_merge==3)
    -----------------------------------------

. 
. * merge in baseline variables
.         merge 1:1 anon_id4 using "$KP_deid_admin/Raw/JPAL_DATA_9/final add baseline_1.dta", nogen upd
> ate keep(1 3 4 5)

    Result                      Number of obs
    -----------------------------------------
    Not matched                     1,871,117
        from master                 1,871,117  
        from using                          0  

    Matched                            17,109
        not updated                         0  
        missing updated                17,109  
        nonmissing conflict                 0  
    -----------------------------------------

. 
.         gen batch = 1

. 
.         gen old_ob_only = 0

.         gen new_ob_only = 0

.         gen match_add_var = 0

.         replace old_ob_only = 1 if _merge == 1
(1,661 real changes made)

.         replace new_ob_only = 1 if _merge == 2
(17,109 real changes made)

.         replace match_add_var = 1 if _merge == 3
(1,869,456 real changes made)

.         drop _merge

. 
. * loop to append each batch
. * Batch 18-22 does not have code_domisili, instead province_code/name, city_code/name
. * Batch 18-22: final_test_score instead of test_score
. * Batch 18:22 ticket_score instaed of bobot
.         qui forval i = 2 / 22 {
Appending Batch 2
Appending Batch 3
Appending Batch 4
Appending Batch 5
Appending Batch 6
Appending Batch 7
Appending Batch 8
Appending Batch 9
Appending Batch 10
Appending Batch 11
Appending Batch 12
Appending Batch 13
Appending Batch 14
Appending Batch 15
Appending Batch 16
Appending Batch 17
Appending Batch 18
Appending Batch 19
Appending Batch 20
Appending Batch 21
Appending Batch 22

. 
. * merge in data transfer 12 - updated treatment status for 2022 control group survey respondents
.         rename batch_sk batch_sk_39

.         merge m:1 anon_id4 using "$KP_deid_admin/Raw/JPAL_DATA_12/[J-PAL] CGS Update Treatment Status
>  Batch 47.dta", gen(merge_t12) assert(1 3)

    Result                      Number of obs
    -----------------------------------------
    Not matched                    58,818,309
        from master                58,818,309  (merge_t12==1)
        from using                          0  (merge_t12==2)

    Matched                            46,540  (merge_t12==3)
    -----------------------------------------

.         rename batch_sk batch_sk_47

. 
. * Check uniqueness
.         gisid anon_id4 batch

.         gsort anon_id4 batch

. 
. * Save raw merged data
. datasignature 
  58864849:50(90903):310014626:1706635967

. if "`r(datasignature)'" == "58864849:50(90903):310014626:1706635967" {
.    save "$KP_deid_admin/Raw/Merged/pmo_b1-22_raw_long.dta", replace
file /proj/pbolken/pkrepkitdata/Data/Admin/Raw/Merged/pmo_b1-22_raw_long.dta saved
.       }

. else {
.    di as err "Careful, your machine produces a different dataset"
.    stop
.                 }

.         
. 
.         cap log close
